﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Globalization;
using System.ComponentModel;
using System.Threading;
using System.Data;
using System.Web;

namespace MixNP.BusinessLayer
{
    public static class Email
    {

        public static bool SendConfirmationEmail(System.Web.UI.Page page, string emailAddress)
        {
            string domain = "MixNP.User.Registration";

            if (IsInQueue(domain, emailAddress))
            {
                return true;
            }

            string name = page.Server.HtmlEncode(MixNP.BusinessLayer.Users.GetNameFromEmailAddress(emailAddress));
            string salt = MixNP.BusinessLayer.Users.GetSaltFromEmailAddress(emailAddress);

            string confirmationLink = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/confirm/" + salt + ".mix";
            string confirmAnchor = string.Format("<a href=\"{0}\" title=\"Confirm your account\">{0}</a>", confirmationLink);
            string cancelLink = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/cancel/" + salt + ".mix";

            string emailBody = System.IO.File.ReadAllText(page.Server.MapPath("~/Classifieds/Templates/Confirmation.html"));
            emailBody = emailBody.Replace("{MemberName}", name);
            emailBody = emailBody.Replace("{ConfirmationLink}", confirmAnchor);
            emailBody = emailBody.Replace("{CancelLink}", cancelLink);

            return MixNP.BusinessLayer.Email.SendEmail(domain, emailAddress, emailAddress, "MixNP Classifieds-Confirm Your Registration", emailBody, "noreply@mixnp.com");
        }

        public static bool SendCommentAlert(System.Web.UI.Page page, double itemId)
        {
            string domain = "MixNP.Alert";

            string emailAddress = MixNP.BusinessLayer.Items.GetAuthorEmail(itemId);

            string title = MixNP.BusinessLayer.Items.GetTitle(itemId);
            string url = page.Request.Url.AbsoluteUri;
            string name = page.Server.HtmlEncode(MixNP.BusinessLayer.Users.GetNameFromEmailAddress(emailAddress));
            string anchor = string.Format("<a href=\"{0}\" title=\"{0}\">{0}</a>", url);


            string emailBody = System.IO.File.ReadAllText(page.Server.MapPath("~/Classifieds/Templates/CommentAdded.htm"));
            emailBody = emailBody.Replace("{Title}", title);
            emailBody = emailBody.Replace("{MemberName}", name);
            emailBody = emailBody.Replace("{Link}", anchor);

            return MixNP.BusinessLayer.Email.SendEmail(domain, emailAddress, emailAddress, "MixNP Classifieds-New Comment Added for " + title, emailBody, "noreply@mixnp.com");
        }


        public static bool SendEmail(string domain, string primaryKeyValue, string recipient, string subject, string body, string sentBy)
        {
            return MixNP.DatabaseLayer.Email.SendEmail(domain, primaryKeyValue, recipient, subject, body, sentBy);
        }

        public static bool IsInQueue(string domain, string primaryKeyValue)
        {
            return MixNP.DatabaseLayer.Email.IsInQueue(domain, primaryKeyValue);
        }

        public static void ProcessEmails()
        {
            using (DataTable table = GetOutgoingEmails())
            {
                foreach (DataRow row in table.Rows)
                {
                    string outgoingEmailId = Pes.Utility.Conversion.TryCastString(row["OutgoingEmailId"]);

                    string emailAddress = Pes.Utility.Conversion.TryCastString(row["EmailAddress"]);
                    string subject = Pes.Utility.Conversion.TryCastString(row["Subject"]);
                    string message = Pes.Utility.Conversion.TryCastString(row["Message"]);
                    string sentBy = Pes.Utility.Conversion.TryCastString(row["SentBy"]);

                    EmailProcessor processor = new EmailProcessor();
                    processor.Send(sentBy, emailAddress, subject, message, outgoingEmailId);
                }
            }
        }

        public static DataTable GetOutgoingEmails()
        {
            return MixNP.DatabaseLayer.Email.GetOutgoingEmails();
        }

        public static bool FlagSent(string outgoingEmailId)
        {
            return MixNP.DatabaseLayer.Email.FlagSent(outgoingEmailId);
        }
    }
}
